/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package assisisw.smartcal.notificationsubsystem;

import assisisw.smartcal.entitysubsystem.Event;
import assisisw.smartcal.entitysubsystem.IEventController;
import assisisw.smartcal.entitysubsystem.Notification;
import assisisw.smartcal.entitysubsystem.User;
import assisisw.smartcal.weathersubsystem.IWeatherCache;
import assisisw.smartcal.weathersubsystem.WeatherData;
import java.util.Date;
import javax.ejb.EJB;
import javax.ejb.Schedule;
import javax.ejb.Singleton;

/**
 *
 * @author Francesco
 */
@Singleton
public class OneDayBeforeReminderBean 
{

    // Instance Fields
    
    /***/
    @EJB ()
    private IEventController eventController ;
    
    /***/
    @EJB ()
    private IWeatherCache weatherCache ;
    
    /***/
    @EJB ()
    private IEntityControllerObserver entityControllerObserver ;
    
    // Instance Methods
    
    public OneDayBeforeReminderBean () {}
    
    @Schedule ( dayOfMonth = "*" , persistent = true )
    public void check () 
    {
        Iterable < Event > candidates ;
        Date today ;
        WeatherData data ;
        today = new Date () ;
        candidates = eventController.findByDate ( today ) ;
        for ( Event e : candidates )
            if ( e.getPlace () != null && e.getPlace ().trim().compareToIgnoreCase ( "" ) != 0 )
            {
                data = weatherCache.getEntry ( e.getPlace () , new Date () , 12 ) ;
                if ( data != null )
                {
                    // update the event
                    for ( User u : e.getInvitedUsers () )
                        entityControllerObserver.addNotification ( Notification.NotificationType.REMINDER , e, u , "Remember of " + e.getName() ) ;
                    entityControllerObserver.addNotification ( Notification.NotificationType.REMINDER , e, e.getOwningCalendar().getOwner() , "Remember of " + e.getName());
                }
            }
    }
    
}
